'''
Created on 31/12/2009

@author: emlyn
'''
from google.appengine.api import users
from WebsiteRec import WebsiteRec
from google.appengine.ext import db

from google.appengine.ext import webapp


import os

class SiteUser(db.Model):
    email = db.StringProperty()
    isglobaladmin = db.BooleanProperty()
    
    def ForceAndGetSiteUser(cls):
        lsiteUser = SiteUser.GetSiteUser()
        if not lsiteUser:
            user = users.get_current_user()
            lsiteUser = SiteUser()
            lsiteUser.email = user.email()
            lsiteUser.isglobaladmin = False
            lsiteUser.put()
        return lsiteUser
    ForceAndGetSiteUser = classmethod(ForceAndGetSiteUser)
        
    def ForceAndGetSiteUserFromEmail(cls, aEmail):
        lsiteUser = SiteUser.GetSiteUserFromEmail(aEmail)
        if not lsiteUser:
            user = users.get_current_user()
            lsiteUser = SiteUser()
            lsiteUser.email = aEmail
            lsiteUser.isglobaladmin = False
            lsiteUser.put()
        return lsiteUser
    ForceAndGetSiteUserFromEmail = classmethod(ForceAndGetSiteUserFromEmail)
        
    def GetSiteUser(cls):
        retval = None
        
        user = users.get_current_user()
        if user:
            retval = SiteUser.GetSiteUserFromEmail(user.email())
            
        return retval
    GetSiteUser = classmethod(GetSiteUser)
        
    def GetSiteUserFromEmail(cls, aEmail):
        retval = None
        
        Users_query = SiteUser.all().filter("email =", aEmail)
        Users = Users_query.fetch(1)
        
        if Users:
            retval = Users[0]
    
        return retval
    GetSiteUserFromEmail = classmethod(GetSiteUserFromEmail)
    
    def AllowedForWebsite(self, website, isadminonly):
        retval = self.isglobaladmin \
                        or \
                    self.email == "emlynoregan@gmail.com" \
                        or \
                    (website.owneremail and (website.owneremail == self.email)) \
                        or \
                    (website.useremails and (not isadminonly) and (self.email in website.useremails)) \
                        or \
                    (website.adminuseremails and (self.email in website.adminuseremails))

        return retval
            
    
    def GetAllWebsites(self, currentwebsite, isadminonly, amount, offset):
        retval = []
    
        if self.isglobaladmin:
            if currentwebsite == None:
                Websites_query = WebsiteRec.all()
            else:
                Websites_query = WebsiteRec.all().filter("sitename !=", currentwebsite.sitename)
                
            retval = Websites_query.fetch(amount, offset)
        else:
            retval = self.GetWebsitesAllowingSelf(currentwebsite, isadminonly, amount, offset)
            
        return retval
          
    
    def GetWebsitesAllowingSelf(self, currentwebsite, isadminonly, amount, offset):
        retval = []
          
        Websites_query = WebsiteRec.all()
        # Websites_query =  Website.all()
          
        baseoffset = 0
        Websites = Websites_query.fetch(1, baseoffset)
          
        while Websites:
            if len(retval) < offset + amount:
                Website = Websites[0]

                lallowed = (currentwebsite == None) or not (currentwebsite.WebsitesAreEqual(Website))
            
                if lallowed:
                    lallowed = self.AllowedForWebsite(Website, isadminonly)

                if lallowed:
                    retval.append(Website)
                #
                if len(retval) < offset + amount:
                    baseoffset = baseoffset + 1
                    Websites = Websites_query.fetch(1, baseoffset)
                else:
                    Websites = None
        
        #  now slice off the offset 
        if offset > 0:
            if offset < len(retval):
                del retval[:offset]
            else:
                retval = []
            
        return retval
    